sap.ui.define([
    "sap/m/bbf41/Module/controller/BaseController",
    "sap/ui/model/json/JSONModel"
], function (BaseController, JSONModel) {
    "use strict";
    var oJSONModel, oView;
    return BaseController.extend("sap.m.bbf41.Module.controller.Casting", {
        onInit: function () {
            oView = this.getView();

            var me = this;
            me.handleRefresh();

            var oIconTabBar = oView.byId('IconTabBar');

            oIconTabBar.attachSelect(function () {
                me.handleRefresh();
            });
        },
        handlePressOpenMenu: function (oEvent) {
            var oButton = oEvent.getSource();
            // create menu only once
            if (!this._menu) {
                this._menu = sap.ui.xmlfragment(
                    "sap.m.bbf41.Module.MenuItemEventing",
                    this
                );
                this.getView().addDependent(this._menu);
            }
            var eDock = sap.ui.core.Popup.Dock;
            this._menu.open(this._bKeyboard, oButton, eDock.BeginTop, eDock.BeginBottom, oButton);
        },
        handleRefresh: function () {
            oJSONModel = this.loadFoundryDataModel();
            oView.setModel(oJSONModel);
        },
        loadFoundryDataModel: function () {
            var me = this,
                oModel = new JSONModel(),
                oView = this.getView(),
                oCastingB38ATable = oView.byId('CastingB38ATable'),
                oCastingB38BTable = oView.byId('CastingB38BTable'),
                oCastingB48ATable = oView.byId('CastingB48ATable'),
                oCastingB48BTable = oView.byId('CastingB48BTable'),
                oMessageStrip = oView.byId('messageStrip');
            oCastingB38ATable.setBusy(true);
            oMessageStrip.setBusy(true);
            jQuery.ajax({
                url: '/NEP/ALS/api/environment_data',
                dataType: "json",
                success: function (resp) {
                    var oData = me.buildTableData(resp);
                    oModel.setData(oData);
                    oView.setModel(oJSONModel);
                }.bind(this),
                error: function () {
                    jQuery.sap.log.error("failed to load json");
                },
                complete: function () {
                    oCastingB38ATable.setBusy(false);
                    oCastingB38BTable.setBusy(false);
                    oCastingB48ATable.setBusy(false);
                    oCastingB48BTable.setBusy(false);
                    oMessageStrip.setBusy(false);
                }
            });
            return oModel;
        },
        onNavButtonPress: function () {
            //window.history.back();
            window.location.href = '/m';
        },
        handleMenuItemPress: function (oEvent) {
            var routeName = oEvent.getSource().data('area');
            this.getRouter().navTo(routeName);
        },
        buildTableData: function (resp) {
            var data = {
                "B38ACollection": [
                    {
                        "Type": "Target",
                        "Shift1": 0,
                        "Shift2": 0,
                        "Shift3": 0,
                        "Today": 0,
                        "CurrentWeek": 0
                    },
                    {
                        "Type": "Current",
                        "Shift1": 0,
                        "Shift2": 0,
                        "Shift3": 0,
                        "Today": 0,
                        "CurrentWeek": 0
                    },
                    {
                        "Type": "Forecast",
                        "Shift1": 0,
                        "Shift2": 0,
                        "Shift3": 0,
                        "Today": 0,
                        "CurrentWeek": 0
                    },
                    {
                        "Type": "Planned",
                        "Shift1": 0,
                        "Shift2": 0,
                        "Shift3": 0,
                        "Today": 0,
                        "CurrentWeek": 0
                    },
                    {
                        "Type": "Deviation",
                        "Shift1": 0,
                        "Shift2": 0,
                        "Shift3": 0,
                        "Today": 0,
                        "CurrentWeek": 0
                    }
                ],
                "B38BCollection": [
                    {
                        "Type": "Target",
                        "Shift1": 0,
                        "Shift2": 0,
                        "Shift3": 0,
                        "Today": 0,
                        "CurrentWeek": 0
                    },
                    {
                        "Type": "Current",
                        "Shift1": 0,
                        "Shift2": 0,
                        "Shift3": 0,
                        "Today": 0,
                        "CurrentWeek": 0
                    },
                    {
                        "Type": "Forecast",
                        "Shift1": 0,
                        "Shift2": 0,
                        "Shift3": 0,
                        "Today": 0,
                        "CurrentWeek": 0
                    },
                    {
                        "Type": "Planned",
                        "Shift1": 0,
                        "Shift2": 0,
                        "Shift3": 0,
                        "Today": 0,
                        "CurrentWeek": 0
                    },
                    {
                        "Type": "Deviation",
                        "Shift1": 0,
                        "Shift2": 0,
                        "Shift3": 0,
                        "Today": 0,
                        "CurrentWeek": 0
                    }
                ],
                "B48ACollection": [
                    {
                        "Type": "Target",
                        "Shift1": 0,
                        "Shift2": 0,
                        "Shift3": 0,
                        "Today": 0,
                        "CurrentWeek": 0
                    },
                    {
                        "Type": "Current",
                        "Shift1": 0,
                        "Shift2": 0,
                        "Shift3": 0,
                        "Today": 0,
                        "CurrentWeek": 0
                    },
                    {
                        "Type": "Forecast",
                        "Shift1": 0,
                        "Shift2": 0,
                        "Shift3": 0,
                        "Today": 0,
                        "CurrentWeek": 0
                    },
                    {
                        "Type": "Planned",
                        "Shift1": 0,
                        "Shift2": 0,
                        "Shift3": 0,
                        "Today": 0,
                        "CurrentWeek": 0
                    },
                    {
                        "Type": "Deviation",
                        "Shift1": 0,
                        "Shift2": 0,
                        "Shift3": 0,
                        "Today": 0,
                        "CurrentWeek": 0
                    }
                ],
                "B48BCollection": [
                    {
                        "Type": "Target",
                        "Shift1": 0,
                        "Shift2": 0,
                        "Shift3": 0,
                        "Today": 0,
                        "CurrentWeek": 0
                    },
                    {
                        "Type": "Current",
                        "Shift1": 0,
                        "Shift2": 0,
                        "Shift3": 0,
                        "Today": 0,
                        "CurrentWeek": 0
                    },
                    {
                        "Type": "Forecast",
                        "Shift1": 0,
                        "Shift2": 0,
                        "Shift3": 0,
                        "Today": 0,
                        "CurrentWeek": 0
                    },
                    {
                        "Type": "Planned",
                        "Shift1": 0,
                        "Shift2": 0,
                        "Shift3": 0,
                        "Today": 0,
                        "CurrentWeek": 0
                    },
                    {
                        "Type": "Deviation",
                        "Shift1": 0,
                        "Shift2": 0,
                        "Shift3": 0,
                        "Today": 0,
                        "CurrentWeek": 0
                    }
                ],
                "Shift1": "Shift 1",
                "Shift2": "Shift 2",
                "Shift3": "Shift 3",
                "LineRate": "0",
                "ShiftNum": 0
            };
            var po,
                dayTotal = 0;

            if (resp && resp['api_data']) {
                $.each(resp['api_data'], function (i, o) {
                    var cKey = i + 'Collection';
                    po = data[cKey];
                    dayTotal = (dayTotal || 0) + parseInt(o[1]['today']);
                    $.each(o, function (ii, oo) {
                        po[ii]['Shift1'] = oo['shift_1'];
                        po[ii]['Shift2'] = oo['shift_2'];
                        po[ii]['Shift3'] = oo['shift_3'];
                        po[ii]['Today'] = oo['today'];
                        po[ii]['CurrentWeek'] = oo['current_week'];
                    });
                });
                data['ShiftNum'] = resp['shift_num'];
                this.calcLineRate(data, dayTotal);
            }
            return data;
        },
        calcLineRate: function (data, dayTotal) {
            var date = new Date(),
                y = date.getFullYear(),
                m = date.getMonth() + 1,
                d = date.getDate(),
                paddingNum = function (n) {
                    if (n < 10) {
                        n = '0' + n;
                    }
                    return n;
                };
            var ds = y + '/' + paddingNum(m) + '/' + paddingNum(d) + ' 06:00:00',
                ts = (new Date(ds)).getTime() / 1000,
                nts = parseInt((new Date()).getTime() / 1000),
                nh = parseInt((nts - ts) / 360) / 10,
                rate = nh != 0 ? parseInt(dayTotal * 100 / nh) / 100 : 0;
            data['LineRate'] = rate + ' U/H';
        }
    });

});
